﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using Tesla.Content;
using Tesla.Graphics;
using Tesla.Scene;

namespace Tesla.Util {
    /// <summary>
    /// Small utility class that loads a material collection and then tries to map materials
    /// to names of IRenderables in a scene hierarchy.
    /// </summary>
    public sealed class MaterialToMeshMapper {
        private ContentManager m_content;
        private MaterialCollection m_materials;

        /// <summary>
        /// Constructs a new MaterialToMeshMapper.
        /// </summary>
        /// <param name="content">Content manager to be used to load the material</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the content manager is null.</exception>
        public MaterialToMeshMapper(ContentManager content) {
            if (content == null) {
                throw new ArgumentNullException("content", "ContentManager cannot be null");
            }
            m_content = content;
        }

        /// <summary>
        /// Loads a material based on a resource name string.
        /// </summary>
        /// <param name="resourceName">Resource to load containing the material.</param>
        /// <returns>True if the material was loaded</returns>
        public bool LoadMaterial(String resourceName) {
            try {
                m_materials = m_content.Load<MaterialCollection>(resourceName);
                if (m_materials.Count > 0) {
                    return true;
                }
            } catch {
                return false;
            }

            return false;
        }

        /// <summary>
        /// Takes a scene hierarchy and tries to map a material to an IRenderable
        /// in the scene based on its name.
        /// </summary>
        /// <param name="spatial">Scene root</param>
        public void MapToMeshes(Spatial spatial) {
            if (m_materials == null || m_materials.Count == 0) {
                return;
            }

            MapToMeshesInternal(spatial);
        }

        private void MapToMeshesInternal(Spatial spatial) {
            if (spatial is Node) {
                Node node = spatial as Node;
                foreach (Spatial child in node) {
                    MapToMeshes(child);
                }
            } else if (spatial is IRenderable) {
                String name = spatial.Name;
                Material mat = m_materials[name];
                if (mat != null) {
                    spatial.SetMaterial(mat);
                }
            }
        }
    }
}
